<template>
  <el-form
    :model="roleForm"
    :rules="rules"
    ref="ruleForm"
    label-width="100px"
    class="demo-ruleForm"
  >
    <el-form-item label="角色" prop="roleName">
      <el-input v-model="roleForm.roleName"></el-input>
    </el-form-item>

    <el-form-item label="描述" prop="roleDesc">
      <el-input v-model="roleForm.roleDesc"></el-input>
    </el-form-item>

    <!-- <el-form-item label="状态" prop="roleStatus">
      <el-input v-model="roleForm.roleStatus"></el-input>
    </el-form-item> -->



    <el-form-item label="状态" prop="roleStatus">
      <el-radio-group v-model="roleForm.roleStatus">
        <el-radio label="1">启用</el-radio>
        <el-radio label="2">禁用</el-radio>
      </el-radio-group>
    </el-form-item>



    <el-form-item>
      <el-button type="primary" @click="submitForm('roleForm')"
        >立即创建</el-button
      >

      <el-button @click="resetForm('ruleForm')">重置</el-button>
    </el-form-item>
  </el-form>
</template>

<script>
export default {
  mounted: function () {//修改之前初始化显示数据

    let roleId = this.$route.params.id;
    console.log("-----------------------------",roleId);
    var app = this;
    if (roleId) {
      app.$httpRequest.get("role/getById/" + roleId).then(function ({ data }) {
        if (data.code == 0) {
         
          app.roleForm = data.role;
          app.roleForm.roleStatus=app.roleForm.roleStatus+"";
         
        } else {
          console.log("回显失败");
        }
      });
    }
  },

  data() {
    return {
      roleForm: {
        id: "",
        roleName: "miaozai",
        roleStatus: "1",
        roleDesc: "hahaha",
      },
    };
  },
  methods: {
    submitForm(formName) {
      var app = this;
      var url = "role/save";
      if (this.roleForm.id) {
        url = "role/update";
      }
      this.$httpRequest.post(url, this.roleForm, {}).then(function ({ data }) {
        if (data.code == 0) {
          app.$message({
            message: "添加成功",
            type: "success",
            duration: 1000,
            onClose: () => {
              app.$router.push("/toRolePage");
            },
          });
        } else {
          app.$message({
            message: "添加失败",
            type: "error",
            duration: 1000,
          });
        }
      });
    },

    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
  },
};
</script>

<style>
</style>